package com.pennphoto.client;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.XMLParser;



/**
 * This class is a thin layer over the Javascript Visualization Toolkit, http://thejit.org
 * 
 * @author zives
 *
 */
public class FriendVisualization {
	
	public static final native void addToGraph(final JavaScriptObject ht, final String json) /*-{
		var content = JSON.parse(json);
		ht.op.sum(content, { type: "fade:con", fps: 4, duration: 1000, hideLabels: true }); 
	}-*/;
	
	/**
	 * Creates a JavaScript Infovis Toolkit hypertree
	 * 
	 * @param json
	 * @return
	 */
	public static final native JavaScriptObject createGraph(final String json, final PennPhoto parent) /*-{
		var content = JSON.parse(json);

		 //init Hypertree
		var ht = new $wnd.$jit.Hypertree({
		    //id of the visualization container
		    injectInto: 'infovis',
		    //By setting overridable=true,
		    //Node and Edge global properties can be
		    //overriden for each node/edge.
		    Node: {
		        //overridable: true,
		        'transform': false,
		        color: "#f00"
		    },
		
		    Edge: {
		        //overridable: true,
		        color: "#f0f"
		    },
		    //calculate nodes offset
		    offset: 0.2,
		    //Change the animation transition type
		    transition: $wnd.$jit.Trans.Back.easeOut,
		    //animation duration (in milliseconds)
		    duration:1000,
		
		    //Attach event handlers on label creation.
		    onCreateLabel: function(domElement, node){
		        domElement.innerHTML = node.name;
		        domElement.style.cursor = "pointer";
		        domElement.onclick = function () {
					parent.@com.pennphoto.client.PennPhoto::drawNodeAndNeighbors(I)(node.id);
					console.debug("Clicked");
		            ht.onClick(node.id, { hideLabels: false });
		
		        };
		    },
		    //This method is called when moving/placing a label.
		    //You can add some positioning offsets to the labels here.
		    onPlaceLabel: function(domElement, node){
		        var width = domElement.offsetWidth;
		        var intX = parseInt(domElement.style.left);
		        intX -= width / 2;
		        domElement.style.left = intX + 'px';
		    },
		
		    onAfterCompute: function(){
		    }
		});
		//load JSON graph.
		ht.loadJSON(content, 1);
		//compute positions and plot
		ht.refresh();
		//end
		ht.controller.onBeforeCompute(ht.graph.getNode(ht.root));
		ht.controller.onAfterCompute();
		return ht;
	}-*/;
	

	/**
	 * XML to JSON converter, from http://davidwalsh.name/convert-xml-json
	 * @param xml
	 * @return
	 */
	public static native JavaScriptObject xmlToJson(final JavaScriptObject xml) /*-{
	  // Create the return object
	  var obj = {};
	
	  if (xml.nodeType == 1) { // element
	    // do attributes
	    if (xml.attributes.length > 0) {
	    obj["@attributes"] = {};
	      for (var j = 0; j < xml.attributes.length; j++) {
	        var attribute = xml.attributes.item(j);
	        obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
	      }
	    }
	  } else if (xml.nodeType == 3) { // text
	    obj = xml.nodeValue;
	  }
	
	  // do children
	  if (xml.hasChildNodes()) {
	    for(var i = 0; i < xml.childNodes.length; i++) {
	      var item = xml.childNodes.item(i);
	      var nodeName = item.nodeName;
	      if (typeof(obj[nodeName]) == "undefined") {
	        obj[nodeName] = @com.pennphoto.client.FriendVisualization::xmlToJson(Lcom/google/gwt/core/client/JavaScriptObject;)(item);
	      } else {
	        if (typeof(obj[nodeName].length) == "undefined") {
	          var old = obj[nodeName];
	          obj[nodeName] = [];
	          obj[nodeName].push(old);
	        }
	        obj[nodeName].push(@com.pennphoto.client.FriendVisualization::xmlToJson(Lcom/google/gwt/core/client/JavaScriptObject;)(item));
	      }
	    }
	  }
	  return obj;
	}-*/;

	public static native JavaScriptObject parseXML(final String xml) /*-{
		if (window.DOMParser) {
			var parser = new DOMParser();
			return parser.parseFromString(xml,"text/xml");
		} else {
			var doc = new ActiveXObject("Microsoft.XMLDOM");
			doc.async="false";
			doc.loadXML(xml);
			return doc;
		}
	}-*/;

}
